package shop.core.common.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.slf4j.Logger;
import shop.core.common.oscache.FrontCache;
import shop.core.common.oscache.ManageCache;

import org.slf4j.LoggerFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;


/**
 * 系统配置加载监听器
 * 
 * @author huangf
 * 
 */
public class SystemListener implements ServletContextListener {
	private static final Logger logger = LoggerFactory.getLogger(SystemListener.class);
	public void contextDestroyed(ServletContextEvent arg0) {
		System.out.println("contextDestroyed....");
	}

	//开启进入监听器初始化
	public void contextInitialized(ServletContextEvent arg0) {
		System.out.println("contextInitialized....");
		try {
//			这种方式适合于采用Spring框架的B/S系统，通过ServletContext对象获取ApplicationContext对象，然后在通过它获取需要的类实例。
			WebApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(arg0.getServletContext());
			FrontCache frontCache = (FrontCache) app.getBean("frontCache");
			ManageCache manageCache = (ManageCache) app.getBean("manageCache");
			frontCache.loadAllCache();
			manageCache.loadAllCache();
			logger.info("系统初始化成功");
		} catch (Exception e) {
			//打印错误的日志
			logger.error("系统初始化失败！"+e.getMessage());
		}
	}
}
